const app = getApp();
const controllers = app.controllers;
const controller=require("../../controllers/Controller").controller
Page({
  data: {
    id:null,
    dataInfo:{},
    scene:""
  },
  onLoad: function (options) {
    if(options.scene){
      this.setData({
        scene:options.scene
      })
    }
    this.data.id = options.id;
    this.setData({
      id:options.id
    })
    // this.getInfo(this.data.id);
    this.getCouponCodeMsg()
  },
  getInfo: function (id) {
    let that = this; 
    return app.requestApi("/api/vip/myDetail",{accountVipId: id}).then(data =>{
      that.setData({
        dataInfo:data
      });
    });

  },
  onBack(){
    wx.navigateBack();
  },

  onCardUse(e){
    var db = e.currentTarget.dataset;
    let that = this;
    let value={}
    value.shopId=this.data.dataInfo.vip.shopId
    value.code=this.data.scene
    app.util.confirm("确认要核销项目吗？").then(()=>{
      return app.requestApi("/api/vip/writeOffGoods",value).then(data =>{
        wx.showToast({
          title: "核销成功",
          icon: 'none',
        });

        setTimeout(function(){
          wx.reLaunch({
            url: '/pages/Index/Index',
          })
        },1000)
      });
    }).then(()=>{
      
      // return this.getCouponCodeMsg();
    });
    
  },

  show(content,title){

    wx.showModal({
      title: title||'温馨提示',
      content: '',
      success (res) {
        if (res.confirm) {
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    });
  },

  //获取优惠券信息
  getCouponCodeMsg(){
    let value={}
    value.code=this.data.scene
    value.shopId=wx.getStorageSync('shopId')
    controller.getCodeMsg(value).then(res=>{
      this.setData({
        dataInfo:res.data
      })
      console.log(res.data)
      if(Object.keys(res.data).length==0){
        wx.showToast({
          icon:"none",
          title: '该码已核销',
        })
        setTimeout(function(){
          wx.reLaunch({
            url: '/pages/Index/Index',
          })
        },1000)
      }
    })
  },

  
  //核销优惠券
  receiveCoupon(e){
    let shopId=wx.getStorageSync('shopId')
    let couponAccountId=e.currentTarget.dataset.id
    let state=e.currentTarget.dataset.state
    let that=this
    if(state!==0){

    }else{
      wx.showModal({
        title: '温馨提示',
        content: '是否确认核销该优惠券？',
        success (res) {
          if (res.confirm) {
            controller.writeOffCoupon({shopId:shopId,couponAccountId:couponAccountId}).then(res=>{
              wx.showToast({
                icon:"none",
                title: '核销成功',
                duration:1500
              })
              setTimeout(function(){
                wx.reLaunch({
                  url: '/pages/Index/Index',
                })
              },1000)
              that.getInfo(that.data.id);
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      });
    }
  }

  

})